function gotoaddPOSPoint(dom){
	var dialog = editAdminDataDialog("left,top",dom,label_add,"/adminconsole/POSManager/PointPos/AddPOSPoint.jsp","addDialog",AddEventClickPOSPoint,loadTaxElementFinish,null);
}
function EditPOSPointDialog(dom,dataset){
	var dialog = editAdminDataDialog("right",dom,label_add,"/adminconsole/POSManager/PointPos/AddPOSPoint.jsp","addDialog",AddEventClickPOSPoint,loadEditPOSPointElementFinish,dataset);
}
function AddEventClickPOSPoint(dialog,dataset){
	var title = dialog.find("#title").val();
	var pospointId = dialog.find("#pospointId").val();
	var description = dialog.find("#description").val();
	var table = dialog.find("#tablepospoint").is(':checked');
	var product = dialog.find("#productpospoint").is(':checked');
	var counter = dialog.find("#counterpospont").is(':checked');
	var listcheckbox = dialog.find('tr [type=checkbox][name=daialogCounter]');
	var listcounter = "";
	var taxuse = "";
	for(var i =0;i<dialog.find("[name=checktaxCate]").size();i++){
		if($(dialog.find("[name=checktaxCate]")[i]).is(":checked")==true){
			taxuse +=  $(dialog.find("[name=checktaxCate]")[i]).attr("id")+",";
		}
	}
	taxuse = taxuse.substring(0, taxuse.length-1);
	for(var i = 0;i<listcheckbox.length;i++){
		if($(listcheckbox[i]).is(':checked') == true){
			listcounter +=  $(listcheckbox[i]).attr("id")+",";
		}
	}
	var id = "";
	var defaultcounter = dialog.find("[id=defaultcounter] option:selected").attr("id");
	var url = "/POSService";
	if(dataset == null){
		var func = "addPosPoint";
	}else{
		var func = "editPosPoint";
		id = dataset.id;
	}
	var data = { func : func,
			id : id,
			title : title,
			pospointId : pospointId ,
			description : description,
			table : table,
			product : product,
			taxuse:taxuse,
			listcounter:listcounter.substring(0,listcounter.length - 1),
			defaultcounter:defaultcounter
		}
	simpleserviceWithArgs(url,data,showPOS,func);
}

function loadTaxElementFinish(dialog){
	dialog.parents(".ui-dialog:first").css("width",600);
	componentLoading($('[name=counterPOS]:visible'));
	setCounter();
	setOption();
}

function loadElementFinish(dialog){
	dialog.parents(".ui-dialog:first").css("width",600);
}

function loadEditPOSPointElementFinish(dialog,dataset){
	cursorDialogResize(dialog,{width:600});
	componentLoading($('[name=counterPOS]:visible'));
	setCounter();
	setOption(dataset);
	EditPOS(dataset.id,dialog);
}

function setCounter(){
	var link = "/counterService";
	var data = { func: "getAllCounter"}
	simpleserviceSyn(link,data,setCounterSuccess);
}
function setCounterSuccess(data){
	$("[id*=counterPOSTr]").attr("style","display:");
	$("[id*=counterpospont]")
	var objArray = JSON.parse(data);
	var textCounter = "";
	var checkTr = 0;
	var rowText = "";
	for(var i=0;i<objArray.length;i++){
		checkTr++;
		rowText += "<td><input type='checkbox' id='"+objArray[i].id+"' name='daialogCounter' onclick='setselectionCounter(id,this);' ></td><td><span id='title"+objArray[i].id+"' >"+objArray[i].title+"</span></td>";
		if(checkTr>=2){
			textCounter += "<tr>"+rowText+"<tr>";
			rowText = "";
			checkTr = 0;
		}
		if(objArray.length==1){
			textCounter = "<tr><td><input type='checkbox' name='daialogCounter' id="+objArray[i].id+"></td><td>"+objArray[i].title+"</td><tr>";
		}
	}
	$("[name=counterPOS]:visible").replaceWith("<div name='counterPOS'><table>"+textCounter+"</table></div>");
}
function setselectionCounter(id,thisElement){
	var listCheckBox = "";
	var checkbox = $('tr [type=checkbox][name=daialogCounter]');
	var selected = $("[id*=defaultcounter]:visible option:selected").attr("id");
	$("[id*=defaultcounter] option").remove();
	$("[id*=defaultcounter]:visible").append('<option>'+ titleDefaultCounter +'</option>');
	for(var i = 0;i<checkbox.length;i++){
		if($(checkbox[i]).is(':checked') == true){
			$("[id*=defaultcounter]:visible").append('<option id="' + $(checkbox[i]).attr("id") + '">' 
			+ $("[id*=title"+$(checkbox[i]).attr("id")+"]").text() + '</option>');
		}
	}
	if(selected!=null)
		$("[id*=defaultcounter]:visible option[id="+selected+"]").attr('selected','selected');
}

function setOption(){
	var url = "/POSCategoriesService";
	var data = { func : "getPOS"
		}
	simpleserviceSyn(url,data,setPOSSuccess);
}
function setPOSSuccess(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var textTax = "";
	for(var i=0;i<objArray.length;i++){
		var symbolSet = "";
		if(objArray[1][i].isPercent==true)
			symbolSet = "%";
		else
			symbolSet = symbol;
		textTax += "<span><input type='checkbox' id="+objArray[1][i].id+" name='checktaxCate'> "+objArray[1][i].title+"("+objArray[1][i].CurrencyValue+" "+symbolSet+")</span><br>";
	}
	if(textTax!=""){
		$("[name=taxPOSAdd]").replaceWith('<tr><td  style="width:22%;text-align:right;">'+taxDialog+'</td><td>'+textTax+'</td><tr>');	
	}else{
		$("[name=taxPOSAdd]").replaceWith('<tr></tr>');
	}
}

function EditPOS(id,dialog){
	if(id != null){
		var url = "/POSService";
		var data = { func : "getPOSPointById",
				id : id
			}
		simpleserviceWithArgs(url,data,editSuccessPoint,dialog);
	}
}
function editSuccessPoint(data,dialog){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	dialog.find("#description").val(objArray['Description']);
	dialog.find("#pospointId").val(objArray['pospointId']);
	dialog.find("#title").val(objArray['Title']);
	if(objArray['table']==true)
		dialog.find("#tablepospoint").attr("checked","checked");
	if(objArray['product']==true)
		dialog.find("#productpospoint").attr("checked","checked");
	//set checkbox
	var counterSplit = objArray.counterString.split(",");
	for(var i=0;i<counterSplit.length;i++){
		var counterDialog = dialog.find("[name=daialogCounter]");
		for(var j=0;j<counterDialog.length;j++){
			if(counterSplit[i]==$(counterDialog[j]).attr("id")){
				$(counterDialog[j]).attr('checked','checked');
			}
		}
	}
	//set option
	var checkbox = dialog.find('tr [type=checkbox][name=daialogCounter]');
	dialog.find("[id=defaultcounter] option").remove();
	dialog.find("[id=defaultcounter]").append('<option>'+ titleDefaultCounter +'</option>');
	for(var i = 0;i<checkbox.length;i++){
		if($(checkbox[i]).is(':checked') == true){
			$("[id*=defaultcounter]:visible").append('<option id="' + $(checkbox[i]).attr("id") + '">' 
			+ $("[id*=title"+$(checkbox[i]).attr("id")+"]").text() + '</option>');
		}
	}
	//set select option
	dialog.find("[id=defaultcounter] option[id="+objArray.defaultCounterString+"]").attr('selected','selected');
	//set tax
	if(objArray.listPOSTaxString!=null){
		var datasplit = objArray.listPOSTaxString.split(",");
		for(var i =0;i<datasplit.length;i++){
			for(var j =0;j<$("[name=checktaxCate]:visible").length;j++){
				if($($("[name=checktaxCate]:visible")[j]).attr("id")==datasplit[i])
					$($("[name=checktaxCate]:visible")[j]).attr('checked', true);
			}
		}
	}
}

function showPOS(id,typeAction){
	var objArray = new Array();
	if(id!=null&&id!='add'&&id!="next"&&id != "previous"){
		objArray = JSON.parse(id);
	}
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	if(id == null||typeAction!=null||id == 'add'){
		if($("[name=maxEntries]").val() == null){
			var line = 10;
		}else{
			var line = $("[name=maxEntries]").val();
		}
		if($("[name=selectpage]").val() == null){
			var pagePointer = 1; 
		}else{
			var pagePointer = $("[name=selectpage]").val(); 
		}
	}else if(id == "next"){
		var line = $("[name=maxEntries]").val();
		var pagePointer = parseInt($("[name=selectpage]").val())+1;
	}else if(id == "previous"){
		var line = $("[name=maxEntries]").val();
		var pagePointer = parseInt($("[name=selectpage]").val())-1;		
	}
	var link = "/POSService";
	var data = { func: "getPOS",
				offset: (line*pagePointer)-line,
				line: line,
				pagePointer: pagePointer
					}
	simpleservice(link,data,showPOSList);
}

function showPOSList(data){
	var objArray2 = new Array();
	objArray2 = JSON.parse(data);
	var sumtext = "";
	var text = "";
	var pageAmount = Math.ceil(objArray2[0]/objArray2[2]);
	var line = objArray2[2];
	var pagePointer = objArray2[1];
	var objArray = objArray2[3];
	$("#listsize").html("("+objArray.length+")");
	for(var i = 0;i< objArray.length+1;i++){
			if(i != objArray.length ){
				if(objArray[i].product==true){
					var porductSet = "<span class='link' onclick='showPOSProduct(\"\",\"\",this,\""+objArray[i].id+"\",\"\",\"\",\"\","+objArray[i].sizeCategory+")' > "+objArray[i].productSize+" Items</span>  /<span class='link' onclick='gotoaddProduct($(this),\"\",\""+objArray[i].title+"\",\""+objArray[i].id+"\");' > Add new </span>";
				}else{
					var porductSet = "";
				}
				if(objArray[i].table==true){
					var zone = "<span class='link' onclick='showPOSZone(\"\",\""+objArray[i].id+"\",this)' > "+objArray[i].zoneSize+" Zone</span>  /<span class='link' onclick='gotoaddZone($(this),\""+objArray[i].id+"\",this);' > Add new </span>";
				}else{
					var zone = "";
				}
				var counter = "<span class='link' onclick='showPOSCounter(\"\",\""+objArray[i].id+"\",this)' > "+objArray[i].counterSize
				+" Counter</span>  /<span class='link' onclick='gotoaddCounter($(this),\""+objArray[i].id+"\",this);' > Add new </span>";
				text = "<tr id=\"row_"+objArray[i]['id']+"\" style=\"border: 0px;\"> " +
						"<td id=\"col_1\" align=\"center\"> "+(i+1)+". </td>"+
						"<td><input type=\"checkbox\" id=\"id_"+i+"\" name=\"ids[]\" value=\""+objArray[i]['id']+"\"> </td>"+
						"<td align=\"left\" name=\"titleName\"> "+objArray[i]['Title']+" </td>"+
						"<td align=\"center\"><span class='link' onclick=\"showPOSCategory('','',this,'',"+objArray[i]['id']
						+",undefined,undefined,'main')\"><span id='count"+objArray[i]['id']+"' >"+objArray[i].sizeCategory+"</span> Items </span>" +
						"/<span class='link' onclick=\"gotoaddCategory($(this),{'idcategory':'pospoint','posId':"+objArray[i]['id']+"},'add');\" > Add new </span></td>"+
						"<td align=\"center\"  >"+porductSet+"</td>"+
						"<td align=\"center\"  >"+zone+"</td>"+
						"<td align=\"center\"  >"+counter+"</td>"+
						"<td align=\"center\"><a href=\"#\" onclick=\"EditPOSPointDialog($(this),{id:"+objArray[i]['id']+",taxId:'"+objArray[i].listPOSTaxString+"'}"+")\">Edit</a></td>"+
						"<td align=\"center\"><a href=\"#\" onclick=\"changeStatusPOSPoint("+objArray[i]['id']+") \"> " +
						"<span id =status"+objArray[i]['id']+">"+objArray[i]['status']+"</a>" +
						"</td>" +
						"</tr>";
				sumtext += text;
			}else{
				text = "<tr>"+
				"<td colspan=\"13\" style=\"padding:0;\">"+
				"<table class=\"tablefoot\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
				"<tbody>"+
					"<tr>"+
						"<td width=\"33%\" align=\"left\">"+foor_page+"&nbsp;"+
							"<select name=\"selectpage\" onchange=\"showTax()\">"+
							"</select>&nbsp;"+foor_of+"&nbsp;<span id=\"pageAmount\">1</span>"+
						"</td>"+
						"<td width=\"31%\" align=\"center\">"+foor_view+
							"<select name=\"maxEntries\" onchange=\"showTax()\">"+
								"<option style=\"color:#000000\"  value=\"10\">10</option>"+
								"<option style=\"color:#000000\"  value=\"20\">20</option>"+
								"<option style=\"color:#000000\"  value=\"30\">30</option>"+
								"<option style=\"color:#000000\"  value=\"50\">50</option>"+
								"<option style=\"color:#000000\"  value=\"70\">70</option>"+
								"<option style=\"color:#000000\"  value=\"100\">100</option>"+
								"<option style=\"color:#000000\"  value=\"200\">200</option>"+
								"<option style=\"color:#000000\"  value=\"100000\">All</option>"+
							"</select>"+foor_rpp+" </td>" +
							"<td width=\"36%\" align=\"right\"><span id=\"previous\">&lt;&lt;"+foor_pre+"</span> |" +
							" <span id=\"next\">"+foor_next+"&gt;&gt;</span></td></tr>"+
							"</tbody></table></td></tr>";
				sumtext += text;
			}
	}
	$("#showList tr").not($("#showList tr").first()).remove();
	$("#showList").append(sumtext);
	$("#pageAmount").text(pageAmount);
	if(pagePointer+1 <= pageAmount){
		$("#next").attr("onclick","showPOS(id)");
		$("#next").html("<a href=\"#\">Next&gt;&gt;</a>");
	}
	if(pagePointer > 1){
		$("#previous").attr("onclick","showPOS(id)");
		$("#previous").html("<a href=\"#\">&lt;&lt;Previous</a>");
	}
	$("[name=maxEntries]").val(line);
	for ( var i = 0; i < pageAmount; i++) {
		$("[name=selectpage]").append(
				$("<option>").val((i+1)).html((i+1)));
		}
	$("[name=selectpage]").val(pagePointer);
	closeWaitingDialog();
}

function changeStatusPOSPoint(id){
	waitingDialog({});
	var url = "/POSService";
	var data = { func : "editStatusPosPoint",
			pospointId : id
		}
	simpleservice(url,data,changeStatusPosPointSuccess);
}

function changeStatusPosPointSuccess(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	$("#status"+objArray["id"]).text(objArray["status"]);
	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
}

function deletePosPoint(){
	waitingDialog({});
	var listCheckBox = "";
	for(var i = 1;i<$('tr [type=checkbox]').not($('tr [type=checkbox]').first()).length;i++){
		if($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i].checked == true){	
			listCheckBox += $($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i]).attr("value")+",";
		}
	}
	var url = "/POSService";
	var data = { func : "deletePosPoint",
			pospointId : listCheckBox.substring(0,listCheckBox.length - 1)
		}
	simpleserviceWithArgs(url,data,showPOS,"Deleted");
}

function refresh(){
	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
	document.location.href ="/adminconsole/RoomsManager/Amenities/Amenities.jsp";
}

function toggleSelect(){
	if($("#showList tr [type=checkbox]")[0].checked == true){
		for(var i = 0;i< $("#showList tr [type=checkbox]").size();i++){
			$("#showList tr [type=checkbox]")[i].checked = true;
		}
	}else{
		for(var i = 0;i< $("#showList tr [type=checkbox]").size();i++){
			$("#showList tr [type=checkbox]")[i].checked = false;
		}
	}
}

function EditSortOder(eleId,id){
	var sortOrder = $("#"+eleId).val();
	var url = "/amenityService";
	var data = { func : "editSortOrder",
			id : id,
			sortOrder: sortOrder
		}
	simpleservice(url,data,refresh);
}